var dbserver = require('../dao/dbserver')
var bcrypt = require('../dao/bcrypt')
var jwt = require('../dao/jwt')
var {apiSuccess,apiError} = require('../dao/apirequest')

var signup = require('../server/signup')
var signin = require('../server/signin')
var search = require('../server/search')
var friend = require('../server/friend')
var user = require('../server/userdetail')
var home = require('../server/home')




module.exports = function(app){

    // ------------------------------------------------
    // 测试接口
    // ------------------------------------------------
    app.get('/test',(req,res)=>res.send("这里是test 路由正常"));
    app.post('/testpost',(req,res)=>res.send("post请求正常"));
    app.get('/testget',(req,res)=>{
        let {body,query,params} = req
        res.send({params,query,body});
    });
    app.get('/aabb',(req,res)=>res.send("这里是aabb"));
    app.get('/testquery',(req,res)=>{
        let {body,query,params} = req
        res.send({params,query,body});
    });
    app.get('/dbtest',(req,res)=>{
        dbserver.findUser(res)
    });
    app.get('/testpwd',(req,res)=>{
        let {pwd} = req.body
        let v = bcrypt.verification(pwd)
        res.send({pwd,v});
    });
    app.post('/testpwd',(req,res)=>{
        let {pwd} = req.body
        console.log('pwd', pwd)
        let v = bcrypt.verification(pwd)
        res.send({pwd,v});
    });

    // ------------------------------------------------
    // 接口
    // ------------------------------------------------

    
    // ----------* 注册页面  signup *---------- 
    /**
     * 
     * @api {post} /signup/add 用户注册
     * @apiName 用户注册
     * @apiGroup signup
     * @apiVersion  0.0.1
     * 
     * 
     * @apiParam  {String} name 用户名
     * @apiParam  {String} mail 邮箱
     * @apiParam  {String} pwd 密码
     * 
     * @apiSuccess (200) {json} result description
     * 
     * @apiParamExample  {json} Request-Example:
     * {
     *   "name":"xiaohong7",
     *   "mail":"xiaohong7@163.com",
     *   "pwd":"12345678"
     * }
     * 
     * 
     * @apiSuccessExample {type} Success-Response:
     * {
     *     "success": true,
     *     "message": "请求成功",
     * }
     * 
     * @apiSampleRequest /signup/add
     * 
     */ 
    app.post('/signup/add',(req,res)=>{
        signup.signUp(req,res);
    })
    
    // 用户或者邮箱判断存在
    /**
     * 
     * @api {post} /signup/jude 注册判断
     * @apiName 判断邮箱，或者用户名存在
     * @apiGroup signup
     * @apiVersion  0.0.1
     * @apiPermission admin
     * 
     * 
     * @apiParam  {String} data 要检查的用户名或邮箱
     * @apiParam  {String} type 要检查的类型 name 或者 email
     * 
     */
    app.post('/signup/jude',(req,res)=>{
        signup.jundeValue(req,res);
    })



    // ----------* 登录页面  signin *---------- 
    /**
     * 
     * @api {post} /signin/match 登陆
     * @apiName 登陆
     * @apiGroup signin
     * @apiVersion  0.0.1
     * 
     * @apiParam  {String} data 用户名
     * @apiParam  {String} pwd  密码
     * 
     */
    app.post('/signin/match',(req,res)=>{
        signin.signIn(req,res);
    })
    /**
     * 
     * @api {post} /signin/verifytoken token解码测试
     * @apiName token解码测试
     * @apiGroup signin
     * @apiVersion  0.0.1
     * 
     * @apiParam  {String} token token值
     * 
     */
    app.post('/signin/verifytoken',(req,res)=>{
        let {token} = req.body
        let jg = jwt.verifyToken(token)

        res.send(apiSuccess(jg))
    })


    // ----------* 搜索页面  search *---------- 
    // 搜索用户
    /**
     * 
     * @api {post} /search/user 搜索用户 
     * @apiName 搜索用户 
     * @apiGroup search
     * @apiVersion  0.0.1
     * 
     * @apiParam  {String} data 要检查的用户名或邮箱
     * @apiParam  {String} type 要检查的类型 name 或者 email（signup/jude 注册判断 接口用到，感觉有功能重复)
     * 
     */
    app.post('/search/user',(req,res)=>{
        search.searchUser(req,res)
    })
    // 判断是否为好友
    /**
     * 
     * @api {post} /search/isfriend 判断是否为好友
     * @apiName 判断是否为好友
     * @apiGroup search
     * @apiVersion  0.0.1
     * 
     * @apiParam  {String} uid 用户id
     * @apiParam  {String} fid 好友id
     * 
     */
    app.post('/search/isfriend',(req,res)=>{
        search.isFriend(req,res)
    })
    // 搜索群
    /**
     * 
     * @api {post} /search/group 搜索群
     * @apiName 搜索群
     * @apiGroup search
     * @apiVersion  0.0.1
     * 
     * @apiParam  {String} data 要检查的群名
     * 
     */
    app.post('/search/group',(req,res)=>{
        search.searchGroup(req,res)
    })
    // 判断是否在群里
    /**
     * 
     * @api {post} /search/isingroup 判断是否在群里
     * @apiName 判断是否在群里
     * @apiGroup search
     * @apiVersion  0.0.1
     * 
     * @apiParam  {String} uid 用户id
     * @apiParam  {String} gid 群id
     * 
     */
    app.post('/search/isingroup',(req,res)=>{
        search.isInGroup(req,res)
    })

    
    // ----------* 用户详情  userdetail *---------- 

    /**
     * 
     * @api {post} /user/detail 用户详情
     * @apiName 用户详情
     * @apiGroup user
     * @apiVersion  0.0.1
     * 
     * @apiParam  {String} uid 用户id
     * 
     */
    app.post('/user/detail',(req,res)=>{
        user.userDetail(req,res)
    })
    /**
     * 
     * @api {post} /user/update 用户信息修改
     * @apiName 用户信息修改
     * @apiGroup user
     * @apiVersion  0.0.1
     * 
     * @apiParam  {String} uid 用户id
     * 
     */
    app.post('/user/update',(req,res)=>{
        user.userUpdate(req,res)
    })
    /**
     * 
     * @api {post} /user/getmarkname 获取好友昵称
     * @apiName 获取好友昵称
     * @apiGroup user
     * @apiVersion  0.0.1
     * 
     * @apiParam  {String} uid 用户id
     * 
     */
    app.post('/user/getmarkname',(req,res)=>{
        user.getMarkName(req,res)
    })
    /**
     * 
     * @api {post} /user/markname 好友昵称修改
     * @apiName 好友昵称修改
     * @apiGroup user
     * @apiVersion  0.0.1
     * 
     * @apiParam  {String} uid 用户id
     * 
     */
    app.post('/user/markname',(req,res)=>{
        user.friendMarkName(req,res)
    })

    // ----------* 好友信息  friend *---------- 
    /**
     * 
     * @api {post} /friend/applyFriend 好友申请
     * @apiName 好友申请
     * @apiGroup friend
     * @apiVersion  0.0.1
     * 
     * @apiParam  {String} uid 用户id
     * @apiParam  {String} fid 好友id
     * @apiParam  {String} msg 申请信息
     * 
     */
    app.post('/friend/applyFriend',(req,res)=>{
        friend.applyFriend(req,res)
    })

    /**
     * 
     * @api {post} /friend/updatefriendstate 更新好友状态
     * @apiName 更新好友状态
     * @apiGroup friend
     * @apiVersion  0.0.1
     * 
     * @apiParam  {String} uid 用户id
     * @apiParam  {String} fid 好友id
     * 
     */
    app.post('/friend/updatefriendstate',(req,res)=>{
        friend.updateFriendState(req,res)
    })

    /**
     * 
     * @api {post} /friend/deletefriendstate 拒绝好友或删除好友
     * @apiName 拒绝好友或删除好友
     * @apiGroup friend
     * @apiVersion  0.0.1
     * 
     * @apiParam  {String} uid 用户id
     * @apiParam  {String} fid 好友id
     * 
     */
    app.post('/friend/deletefriendstate',(req,res)=>{
        friend.deleteFriendState(req,res)
    })

    // ----------* 主页  home *---------- 
    /**
     * 
     * @api {post} /home/getfriend 获取好友列表
     * @apiName 获取好友列表
     * @apiGroup home
     * @apiVersion  0.0.1
     * 
     * @apiParam  {String} uid 用户id
     * @apiParam  {String} state  状态
     * 
     */
    app.post('/home/getfriend',(req,res)=>{
        home.getFriend(req,res)
    })

    /**
     * 
     * @api {post} /home/getlastmsg  获取最后一条消息
     * @apiName  获取最后一条消息
     * @apiGroup home
     * @apiVersion  0.0.1
     * 
     * @apiParam  {String} uid 用户id
     * @apiParam  {String} fid 好友id
     * 
     */
    app.post('/home/getlastmsg',(req,res)=>{
        home.getLastMsg(req,res)
    })
    /**
     * 
     * @api {post} /home/unreadmsg  未读消息数
     * @apiName  未读消息数
     * @apiGroup home
     * @apiVersion  0.0.1
     * 
     * @apiParam  {String} uid 用户id
     * @apiParam  {String} fid 好友id
     * 
     */
    app.post('/home/unreadmsg',(req,res)=>{
        home.unreadMsg(req,res)
    })
    /**
     * 
     * @api {post} /home/unreadmsg  好友消息全部已读
     * @apiName  好友消息全部已读
     * @apiGroup home
     * @apiVersion  0.0.1
     * 
     * @apiParam  {String} uid 用户id
     * @apiParam  {String} fid 好友id
     * 
     */
    app.post('/home/updatemsg',(req,res)=>{
        home.updateMsg(req,res)
    })


}